<script setup lang="ts">
import { getHomeBannerAPI, getHomeCategoryMutliAPI, getHomeHotMutliAPI } from "@/services/home";
import { ref } from 'vue'
import type { BannerItem, CategoryItem, HotItem } from "@/types/home";
import { onLoad } from "@dcloudio/uni-app";
import CustomNavbar from "./components/CustomNavbar.vue";
import CategoryPanel from "./components/CategoryPanel.vue";
import HotPanel from './components/HotPanel.vue';
import PageSkeleton from './components/PageSkeleton.vue';
import type { XtxGuessInstance } from '@/types/component'
import { useGuessList } from "@/composables";

// 定义轮播图数据
const bannerList = ref<BannerItem[]>([])

// 定义分类数据
const CategoryItemList = ref<CategoryItem[]>([])

const HotItemList = ref<HotItem[]>([])

// const guessRef = ref<XtxGuessInstance>()

const isTriggered = ref(false)

const isPageSkeleton = ref(true)


const getHomeBanner = async () => {
  const res = await getHomeBannerAPI()
  bannerList.value = res.result
}

const getHomeCategoryMutli = async () => {
  const res = await getHomeCategoryMutliAPI()
  CategoryItemList.value = res.result
}

const getHomeHotMutli = async () => {
  const res = await getHomeHotMutliAPI()
  HotItemList.value = res.result
}


const { guessRef, onScrolltolower } = useGuessList()

const refresherrefresh = async () => {
  isTriggered.value = true
  guessRef.value?.resetData()
  await Promise.all([
    getHomeBanner(),
    getHomeCategoryMutli(),
    getHomeHotMutli(),
  ])
  isTriggered.value = false
}

onLoad(async () => {
  Promise.all([getHomeBanner(), getHomeCategoryMutli(), getHomeHotMutli()])
  isPageSkeleton.value = false
})

</script>

<template>
  <CustomNavbar />
  <!-- 滚动容器 -->
  <scroll-view scroll-y @scrolltolower="onScrolltolower" class="scroll-view" @refresherrefresh="refresherrefresh"
    :refresher-triggered="isTriggered" refresher-enabled>
    <PageSkeleton v-if="isPageSkeleton" />
    <template v-else>
      <XtxSwiper :list="bannerList" />
      <CategoryPanel :list="CategoryItemList" />
      <HotPanel :list="HotItemList" />
      <XtxGuess ref="guessRef" />
    </template>
  </scroll-view>
</template>

<style lang="scss">
//

page {
  background-color: #f7f7ff;
  height: 100%;
  display: flex;
  flex-direction: column;
}

.scroll-view {
  flex: 1;
}
</style>
